package com.cuz.daileetcode;

import java.util.*;

/**
 * @author cuzz
 * @version 1.0
 * @description: 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * <p>
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * <p>
 * <p>
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * 示例 2：
 * <p>
 * 输入：digits = ""
 * 输出：[]
 * 示例 3：
 * <p>
 * 输入：digits = "2"
 * 输出：["a","b","c"]
 *  
 * <p>
 * 提示：
 * <p>
 * 0 <= digits.length <= 4
 * digits[i] 是范围 ['2', '9'] 的一个数字。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @date 21:49 2021/10/2
 **/
public class Day14_2 {
    public static void main(String[] args) {
        S1 s1 = new S1();
        List<String> strings = s1.letterCombinations("23");

        System.out.println(strings);
    }
    static class S1 {
        public List<String> letterCombinations(String digits) {
            List<String> res = new ArrayList<>();
            if (digits == null || "".equals(digits)) {
                return res;
            }
            Map<Character, String> phoneWordMap = initPhoneWordMap();
            backTrace(res, digits, 0, phoneWordMap, new StringBuilder());
            return res;
        }


        public void backTrace(List<String> res, String digits, int nowIndex, Map<Character, String> phoneWordMap, StringBuilder strTemp) {
            if (nowIndex == digits.length()) {
                res.add(strTemp.toString());
                return;
            }
            char letter = digits.charAt(nowIndex);
            String word = phoneWordMap.get(letter);
            for (int index = 0; index < word.length(); index++) {
                strTemp.append(word.charAt(index));
                backTrace(res, digits, nowIndex + 1, phoneWordMap, strTemp);
                strTemp.deleteCharAt(nowIndex);
            }
        }

        public Map<Character, String> initPhoneWordMap() {
            Map<Character, String> phoneMap = new HashMap<Character, String>(8) {{
                put('2', "abc");
                put('3', "def");
                put('4', "ghi");
                put('5', "jkl");
                put('6', "mno");
                put('7', "pqrs");
                put('8', "tuv");
                put('9', "wxyz");

            }};
            return phoneMap;
        }
    }


}
